package com.hsmw.api.service;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hsmw.api.config.jwt.CurrentHsmwUserUtils;
import com.hsmw.api.config.jwt.LoginUser;
import com.hsmw.api.mapper.*;
import com.hsmw.common.base.BaseService;
import com.htn.common.core.base.DataResponse;
import com.hsmw.domain.*;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class HsmwIntegralGoodsService extends BaseService<HsmwIntegralGoodsMapper, HsmwIntegralGoods> {


    @Autowired
    private HsmwIntegralGoodsMapper hsmwIntegralGoodsMapper;

    @Autowired
    private HsmwIntegraGoodsSkuMapper hsmwIntegraGoodsSkuMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private HsmwUserDriverMapper hsmwUserDriverMapper;

    @Autowired
    private HsmwOrderVehicleRelationMapper hsmwOrderVehicleRelationMapper;

    @Autowired
    private CurrentHsmwUserUtils currentHsmwUserUtils;

    @Autowired
    private HsmwGoodsOrderMapper hsmwGoodsOrderMapper;



    public List<HsmwIntegralGoods> findGoodsList(HsmwIntegralGoods hsmwIntegralGoods) {
        LoginUser loginUser = currentHsmwUserUtils.getCurrentUserInfo();
        HsmwUserDriver userDriver = hsmwUserDriverMapper.selectOne(new LambdaQueryWrapper<HsmwUserDriver>().eq(HsmwUserDriver::getUserId, loginUser.getId()));

        List<HsmwIntegralGoods> hsmwIntegralGoodsList = hsmwIntegralGoodsMapper.selectGoodsList(hsmwIntegralGoods);
        hsmwIntegralGoodsList.stream().forEach(integralGoods ->{
            String sumGoodsAmount = hsmwGoodsOrderMapper.queryGoodsOrderSumGoodsAmount(integralGoods.getId());
            String goodsAmount = hsmwGoodsOrderMapper.queryDriverExchangeGoodsAmount(integralGoods.getId(),userDriver.getId());
            integralGoods.setGoodsAmount(goodsAmount);
            integralGoods.setSumGoodsAmount(sumGoodsAmount);
        });
        return hsmwIntegralGoodsList;
    }

    public HsmwIntegralGoods findGoodsInfoById(String goodsId) {

        HsmwIntegralGoods hsmwIntegralGoods = hsmwIntegralGoodsMapper.selectById(goodsId);

        return hsmwIntegralGoods;
    }

    public List<HsmwIntegraGoodsSku> selectGoodsPageInfo(String goodsId) {

        List<HsmwIntegraGoodsSku> hsmwIntegraGoodsSkus = hsmwIntegraGoodsSkuMapper.selectList(new LambdaQueryWrapper<HsmwIntegraGoodsSku>().eq(HsmwIntegraGoodsSku::getGoodsId, goodsId));
        return hsmwIntegraGoodsSkus;
    }


    /**
     * 赚积分检测
     * @param id
     * @return
     */
    public Map findDriverIntegralStatusById(String id) {

        Map map = new HashMap();
        String integralKey = "driver:integral"+id;
        String bookKey = "driver:book:"+id;

        Long expire = redisTemplate.opsForValue().getOperations().getExpire(bookKey);
        System.err.println(expire);


        Boolean integralFlag = redisTemplate.hasKey(integralKey);
        Boolean bookFlag = redisTemplate.hasKey(bookKey);
        if(integralFlag){
            map.put("integral",integralFlag);
        }else {
            map.put("integral",integralFlag);
        }
        if(bookFlag){
            map.put("book",bookFlag);
        }else {
            map.put("book",bookFlag);
        }
        HsmwUserDriver hsmwUserDriver = hsmwUserDriverMapper.selectById(id);
        String firstOrder = hsmwUserDriver.getFirstOrder();
        if(StringUtils.equals("1",firstOrder)){
            map.put("firstOrder",true);
        }else {
            map.put("firstOrder",false);
        }
        List<HsmwOrderVehicleRelation> hsmwOrderVehicleRelations = hsmwOrderVehicleRelationMapper.selectList(new LambdaQueryWrapper<HsmwOrderVehicleRelation>().eq(HsmwOrderVehicleRelation::getVehicleId, hsmwUserDriver.getVehicleId()));
        if(hsmwOrderVehicleRelations.size() == 1){
            map.put("firstReceiveOrder",true);
        }else {
            map.put("firstReceiveOrder",false);
        }
        if(hsmwOrderVehicleRelations.size() >= 1){
            map.put("firstCompleteOrder",true);
        }else {
            map.put("firstCompleteOrder",false);
        }
        return  map;
    }
}
